<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        客户
        <el-input style="width: 10%" placeholder="请输入姓名" v-model="dataForm.name" clearable></el-input>设计师
        <el-input style="width: 10%" placeholder="请输入" v-model="dataForm.username" clearable></el-input>
        <el-form-item label="类型" prop="type">
          <el-select v-model="dataForm.type" placeholder="请选择" filterable clearable>
            <el-option v-for="item in typeOptions" :key="item.dictionaryCode" :label="item.dictionaryName"
              :value="item.dictionaryCode"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="完成状态" prop="status">
          <el-select v-model="dataForm.status" placeholder="请选择" clearable>
            <el-option label="未完成" :value="0"></el-option>
            <el-option label="已完成" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <br />
        <br />
        <span>时间</span>
        <el-date-picker v-model="dataForm.startDate" type="date" placeholder="开始日期" value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"></el-date-picker>
        <span>至</span>
        <el-date-picker v-model="dataForm.endDate" type="date" placeholder="结束日期" value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"></el-date-picker>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          <el-button type="primary" @click="finishHandle()" :disabled="dataListSelections.length <= 0">批量完成</el-button>
          <el-button type="primary" @click="unfinishHandle()" :disabled="dataListSelections.length <= 0">批量未完成
          </el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="主键">
            </el-table-column>-->
      <el-table-column prop="name" header-align="center" align="center" label="客户"></el-table-column>
      <el-table-column prop="supervisionType" header-align="center" align="center" label="督导类型"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="督导人员"></el-table-column>
      <el-table-column prop="startTime" header-align="center" align="center" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" header-align="center" align="center" label="结束时间"></el-table-column>
      <el-table-column prop="nextTime" header-align="center" align="center" label="下次跟进时间"></el-table-column>
      <!-- <el-table-column
        prop="url"
        header-align="center"
        align="center"
        label="附件">
            </el-table-column>-->
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <template v-if="scope.row.status === 0">未完成</template>
          <template v-else-if="scope.row.status === 1">已完成</template>
          <template v-else></template>
        </template>
      </el-table-column>
      <!--  <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="createId"
        header-align="center"
        align="center"
        label="创建人ID">
      </el-table-column>
      <el-table-column
        prop="created"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="modifier"
        header-align="center"
        align="center"
        label="修改人ID">
      </el-table-column>
      <el-table-column
        prop="modified"
        header-align="center"
        align="center"
        label="最后修改时间">
      </el-table-column>
      <el-table-column
        prop="delVal"
        header-align="center"
        align="center"
        label="删除标示（0--未删除，1--已删除）">
            </el-table-column>-->
      <el-table-column fixed="right" header-align="center" align="center" width="180" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0 || scope.row.status == null" type="text" size="small"
            @click="finishHandle(scope.row.id)">完成</el-button>
          <el-button v-if="scope.row.status == 1" type="text" size="small" @click="unfinishHandle(scope.row.id)">未完成
          </el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from "@/page/basicinfoManagement/supervision-add-or-update";
  export default {
    data() {
      return {
        baseURL: process.env.VUE_APP_BASE_API,
        typeOptions: [],
        dataForm: {
          key: "",
          name: "",
          username: "",
          title: "",
          type: "",
          stage: "",
          status: "",
          startDate: "",
          endDate: ""
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      };
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList();
    },
    mounted() {
      this.getTypeOptionsList();
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        this.$http({
          url: `${this.baseURL}/api/user-service/v1/privilege/getMenuTree`,
          method: "get",
          params: {
            page: this.pageIndex,
            limit: this.pageSize,
            key: this.dataForm.key,
            name: this.dataForm.name,
            type: this.dataForm.type,
            stage: this.dataForm.stage,
            username: this.dataForm.username,
            status: this.dataForm.status,
            title: this.dataForm.title,
            startDate: this.dataForm.startDate,
            endDate: this.dataForm.endDate
          }
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] :
          this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: `${this.baseURL}/api/user-service/v1/privilege/getMenuTree`,
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },

      // 完成
      finishHandle(id) {
        var ids = id ? [id] :
          this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "完成" : "批量完成"}]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: `${this.baseURL}/api/user-service/v1/privilege/getMenuTree`,
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
      // 未完成
      unfinishHandle(id) {
        var ids = id ? [id] :
          this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "未完成" : "批量未完成"}]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: `${this.baseURL}/api/user-service/v1/privilege/getMenuTree`,
            method: "post",
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
      //客户类型从数据字典中取值
      getTypeOptionsList() {
        this.dataListLoading = true;
        this.$http({
          url: `${this.baseURL}/api/user-service/v1/privilege/getMenuTree`,
          method: "get",
          params: {
            type: "supervision_type"
          }
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.typeOptions = data.labels;
          } else {
            this.dataList = [];
          }
          this.dataListLoading = false;
        });
      },
    }
  };
</script>
